package com.cch.cooperation.api.auth.controller;


import com.cch.cooperation.api.auth.dto.*;
import com.cch.cooperation.api.auth.service.UserServiceImpl;
import com.cch.cooperation.api.common.sign.SignValid;
import com.cch.cooperation.api.common.request.IdReq;
import com.cch.cooperation.common.web.response.Response;
import com.cch.cooperation.common.web.session.Session;
import com.cch.cooperation.common.web.session.SessionManager;
import com.cch.cooperation.dao.auth.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * 用户接口
 *
 * @author cch
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/api/auth/user")
public class UserController {

    @Resource
    private UserServiceImpl userService;
    @Resource
    private SessionManager sessionManager;

    @ApiOperation("根据短信验证码登陆")
    @PostMapping("/loginBySmsCode")
    public Response<Session<LoginUserDTO>> loginBySmsCode(@RequestBody LoginReqDTO req) {
        LoginUserDTO user = userService.loginBySmsCode(req);
        Session session = new Session(UUID.randomUUID().toString(), user.getId(), user);
        sessionManager.add(session);
        return Response.success(session);
    }

    @ApiOperation("使用 Apple 授权码登陆")
    @PostMapping("/loginByApple")
    public Response<Session<LoginUserDTO>> loginByApple(@RequestBody AppleLoginReqDTO req) throws Exception {
        LoginUserDTO user = userService.loginByApple(req);
        Session session = new Session(UUID.randomUUID().toString(), user.getId(), user);
        sessionManager.add(session);
        return Response.success(session);
    }

    @ApiOperation("游客登陆")
    @PostMapping("/loginByTourist")
    public Response<Session<LoginUserDTO>> loginByTourist(@RequestBody IdReq req) {
        LoginUserDTO user = userService.loginByTourist(req);
        Session session = new Session(UUID.randomUUID().toString(), user.getId(), user);
        sessionManager.add(session);
        return Response.success(session);
    }

    @ApiOperation("手机号一键登录")
    @PostMapping("/loginByPhoneNumber")
    public Response<Session<LoginUserDTO>> loginByPhoneNumber(@RequestBody PhoneNumberLoginReqDTO req) {
        LoginUserDTO user = userService.loginByPhoneNumber(req);
        Session session = new Session(UUID.randomUUID().toString(), user.getId(), user);
        sessionManager.add(session);
        return Response.success(session);
    }

    @ApiOperation("绑定手机号")
    @PostMapping("/bindPhone")
    public Response<Session<LoginUserDTO>> bindPhone(@RequestBody BindPhoneReqDTO req) {
        Session<LoginUserDTO> session = sessionManager.get();
        req.setId(session.getData().getId());
        userService.bindPhoneNumber(req);
        session.getData().setUsername(req.getPhoneNumber());
        return Response.success(session);
    }

    @ApiOperation("账号注销")
    @PostMapping("/cancellation")
    public Response<Void> cancellation() {
        Session<LoginUserDTO> session = sessionManager.get();
        userService.cancellation(session.getData().getId());
        logout();
        return Response.success();
    }

    @ApiOperation("修改用户信息")
    @PostMapping("/update")
    public Response<Session<LoginUserDTO>> update(@RequestBody UserUpdateDTO req) {
        Session<LoginUserDTO> session = sessionManager.get();
        req.setId(session.getData().getId());
        LoginUserDTO user = userService.updateById(req);
        session.setData(user);
        sessionManager.add(session);
        return Response.success(session);
    }

    @ApiOperation("获取当前登陆用户信息")
    @GetMapping("/getSession")
    public Response<Session<LoginUserDTO>> getSession() {
        Session<LoginUserDTO> data = sessionManager.get();
        return Response.success(data);
    }

    @ApiOperation("刷新当前登陆用户 Token 过期时间")
    @GetMapping("/refreshToken")
    public Response<Session<LoginUserDTO>> refreshToken() {
        Session<LoginUserDTO> data = sessionManager.get();
        sessionManager.update(data);
        return Response.success(data);
    }

    @ApiOperation("退出登陆")
    @GetMapping("/logout")
    public Response<Void> logout() {
        Session<LoginUserDTO> data = sessionManager.get();
        sessionManager.remove(data.getAccessToken());
        return Response.success();
    }

    @ApiOperation("加入黑名单")
    @PostMapping("/addBlacklist")
    public Response<Void> addBlacklist(@RequestBody IdReq req) {
        Session<LoginUserDTO> data = sessionManager.get();
        userService.addBlacklist(data.getData().getId(), req.getId());
        return Response.success();
    }

    @ApiOperation("移除黑名单")
    @PostMapping("/deleteBlacklist")
    public Response<Void> deleteBlacklist(@RequestBody IdReq req) {
        Session<LoginUserDTO> session = sessionManager.get();
        userService.deleteBlacklist(session.getData().getId(), req.getId());
        return Response.success();
    }

    @ApiOperation("查询黑名单列表")
    @GetMapping("/queryBlacklist")
    public Response<List<User>> queryBlacklist() {
        Session<LoginUserDTO> session = sessionManager.get();
        List<User> data = userService.queryBlacklist(session.getData().getId());
        return Response.success(data);
    }

    @ApiOperation("会员续期")
    @SignValid
    @PostMapping("/renewVip")
    public Response<Session<LoginUserDTO>> renewVip(@RequestBody VipRenewDTO req) {
        Session<LoginUserDTO> session = sessionManager.get();
        req.setUserId(session.getData().getId());
        User data = userService.renewVip(req);
        session.getData().setVipExpireTime(data.getVipExpireTime());
        return Response.success(session);
    }

}
